package com.wkhyapp.lm.view;

import android.content.Intent;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wkhyapp.lm.MainActivity;
import com.wkhyapp.lm.R;
import com.wkhyapp.lm.base.BasePresenter;
import com.wkhyapp.lm.base.SuperActivity;
import com.wkhyapp.lm.contract.PhoneLoginPresenter;
import com.wkhyapp.lm.contract.PhoneLoginView;
import com.wkhyapp.lm.http.vo.Member;
import com.wkhyapp.lm.utils.MemberUtils;
import com.wkhyapp.lm.utils.PreferenceUtils;
import com.wkhyapp.lm.utils.StatusBarFontUtil;
import com.wkhyapp.lm.wxapi.WXEntryActivity;

import butterknife.BindView;

public class PhoneLoginActivity extends SuperActivity<PhoneLoginPresenter> implements PhoneLoginView {




    @BindView(R.id.back_iv)
    ImageView back_iv;
    @BindView(R.id.acconut_et)
    EditText acconut_et;
    @BindView(R.id.pwd_et)
    EditText pwd_et;
    @BindView(R.id.login_rl)
    RelativeLayout login_rl;
    @BindView(R.id.wjmm_tv)
    TextView wjmm_tv;
    @BindView(R.id.yhxy_tv)
    TextView yhxy_tv;
    @BindView(R.id.yscl_tv)
    TextView yscl_tv;
    @BindView(R.id.login_cb)
    CheckBox login_cb;
    @BindView(R.id.tips_iv)
    TextView tips_iv;
    @Override
    public void initUI() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            translucentStatusBar();
        }
        StatusBarFontUtil.setLightStatusBarColor(this);


    }

    @Override
    protected void initListener() {
        back_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        login_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(!login_cb.isChecked()){
                    VISIBLE(tips_iv);
                    return;
                }

                if("".equals(acconut_et.getText().toString())){
                    TToast("请输入手机号码");
                    return;
                }
                if("".equals(pwd_et.getText().toString())){
                    TToast("请输入密码");
                    return;
                }

                showLoad("正在登陆");
                mPresenter.login(acconut_et.getText().toString(),pwd_et.getText().toString());
            }
        });

        wjmm_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if("".equals(acconut_et.getText().toString())){
                    TToast("请输入手机号码");
                    return;
                }
                if(acconut_et.getText().toString().length()!=11){
                    TToast("请输入正确的手机号");
                    return;
                }
                goTo(VerCodeActivity.class,acconut_et.getText().toString());
            }
        });

        yhxy_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goTo(WebViewActivity.class,"http://img.wkhyw.com/yhxy.html","用户协议");
            }
        });

        yscl_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goTo(WebViewActivity.class,"http://img.wkhyw.com/yssm.html","隐私声明");
            }
        });

        login_cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    GONE(tips_iv);
                }
            }
        });

    }

    @Override
    protected void afterCreate() {

    }
    @Override
    public void loginSucc(Member member) {
        MemberUtils.saveMember(member);
        PreferenceUtils.getInstance().setPrefInt("userId",member.getId());
        Intent intent = new Intent(PhoneLoginActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void loginFial(String str) {
        TToast(str);
    }

    @Override
    public void getCode(String str) {

    }

    @Override
    public void verSucc(String str) {

    }


    @Override
    protected PhoneLoginPresenter createPresenter() {
        return mPresenter = new PhoneLoginPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_phone_login;
    }

}
